﻿Imports Janus.Windows.GridEX
Imports OutlookStyleControls
Imports System.Collections
Imports System.ComponentModel

Public Class frmQuanLyLoHang
    'dung de luu gia tri loc cua lo hang
    'sau do truyen vao form Loc 
    Public fCallPhieuNX As Form = Nothing

    Private mMaHang As String = ""
    Private mTenHang As String = ""
    Private mLoHang As String = ""
    Private mCCL As String = ""
    Private mSSHSD As String = ""
    Private mHSD As Date = Now.Date
    Private mSSSLTon As String = ""
    Private mSLTon As String = ""

    Private mDMLoaiKho As String = ""
    Private mKho As String = ""
    Dim flagHSD As Boolean = False

#Region "Property"
    Property MaHang() As String
        Get
            Return mMaHang
        End Get
        Set(ByVal value As String)
            mMaHang = value
        End Set
    End Property
    Property TenHang() As String
        Get
            Return mTenHang
        End Get
        Set(ByVal value As String)
            mTenHang = value
        End Set
    End Property
    Property LoHang() As String
        Get
            Return mLoHang
        End Get
        Set(ByVal value As String)
            mLoHang = value
        End Set
    End Property
    Property CCL() As String
        Get
            Return mCCL
        End Get
        Set(ByVal value As String)
            mCCL = value
        End Set
    End Property
    Property SSHSD() As String
        Get
            Return mSSHSD
        End Get
        Set(ByVal value As String)
            mSSHSD = value
        End Set
    End Property
    Property HSD() As Date
        Get
            Return mHSD
        End Get
        Set(ByVal value As Date)
            mHSD = value
        End Set
    End Property
    Property SSSLTon() As String
        Get
            Return mSSSLTon
        End Get
        Set(ByVal value As String)
            mSSSLTon = value
        End Set
    End Property
    Property SLTon() As String
        Get
            Return mSLTon
        End Get
        Set(ByVal value As String)
            mSLTon = value
        End Set
    End Property
    Property DMLoaiKho() As String
        Get
            Return mDMLoaiKho
        End Get
        Set(ByVal value As String)
            mDMLoaiKho = value
        End Set
    End Property
#End Region

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        KhoiTaoDMKho()
    End Sub

    Public Sub New(ByVal ma_loaikho As String)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mDMLoaiKho = ma_loaikho
        KhoiTaoDMKho()
        'CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.KHO, cbbKhoHang.SelectedIndex).Ten

        'KhoiTaoDMLoHang(GetstrLoc())

    End Sub

    Public Sub New(ByVal ma_loaikho As String, ByVal maloaithuoc As String)
        'dung khoi tao dm lo hang cho phieu xuat
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mDMLoaiKho = ma_loaikho
        ''''''''''''''''''''''''''''''''''''''''''''''''''''Ghi Chu
        ''''''''''''''''''''''''''''''''''''''''''''''''''''Sua
        'bo loc theo loai thuoc (yeu cau)
        'mMaHang = maloaithuoc
        'loc nhung lo hang theo loai kho, loai mat hang va co SLTon > 0
        mSSSLTon = ">"
        mSLTon = 0
        KhoiTaoDMKho()
    End Sub

    Public Sub New(ByVal ma_loaikho As String, ByVal ma_kho As String, ByVal mathang_id As Integer)
        'dung khoi tao dm lo hang cho phieu xuat
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mDMLoaiKho = ma_loaikho
        'loc nhung lo hang theo loai kho, loai mat hang va co SLTon > 0
        'mSSSLTon = ">"
        'mSLTon = 0
        KhoiTaoDMKho()
        cbbKhoHang.SelectedValue = ma_kho
        KhoiTaoDMLoHang(GetstrLoc())
        lbxLoHang.Focus()
        lbxLoHang.SelectedValue = mathang_id
    End Sub


#Region "KhoiTao"
    Private Sub KhoiTaoDMKho()
        'GetDonVi_KhoListByStrDMKho
        'Dim strSql As String
        'strSql = "select L,KH,Ten,NH from tblDMKhac where KH = 'K'" '& strWhereDMKhoByUser
        'Dim aryKH As ArrayList = CDoiTuongs.GetDoiTuongListBySql(strSql)
        Dim aryKH As ArrayList = CDoiTuongs.GetDoiTuongList(enumDOITUONG.KHO)
        If aryKH.Count > 0 Then
            cbbKhoHang.DataSource = aryKH
            cbbKhoHang.ValueMember = "KH"
            cbbKhoHang.DisplayMember = "Ten"
        End If

        If (aryKH.Count <> 0) Then
            cbbKhoHang.SelectedIndex = 0
        End If
    End Sub



    Private Sub KhoiTaoDMLoHang(ByVal strwhere As String)
        Dim aryDTs As ArrayList = CMatHangs.GetMatHangList(strwhere)
        lbxLoHang.DataSource = aryDTs
        lblTongMatHang.Text = lbxLoHang.Items.Count.ToString + " Mặt Hàng"
        If (lbxLoHang.Items.Count <> 0) Then
            lbxLoHang.ValueMember = "mathang_id"
            lbxLoHang.DisplayMember = "ten"
            lbxLoHang.SelectedIndex = 0
        Else
            txtTenHang.Text = ""
            txtMa_Lo.Text = ""
            txtNgTTon.Text = ""
            txtCCL.Text = ""
            txtHSD.Text = ""
            txtDonGia.Text = ""
            txtTonKH.Text = ""
            txtTonTH.Text = ""
        End If
    End Sub

    Public Sub KhoiTaodtgPhieuNhapXuat(ByVal MATHANG_ID As String)
        'nen  tao view phieunhap va view phieu xuat
        'khoi tao dtgPhieuNhap
        Dim strSql As String
        'strSql = "select NX, (case when(bcth = 1)then 'KH' else 'TC' end) as TT ,sophieu, (CAST(day(NGAY) AS nvarchar(2)) + '/' + cast(month(NGAY)as nvarchar(2)) + '/'+ cast(YEAR(NGAY) as nvarchar(4))) as Ngay, tendvgn, tentcnx, dbo.ChuanHoaSo(slkh) as slkh, dbo.ChuanHoaSo(slth) as slth from viewNhapXuat " & _
        '                          "where MATHANG_ID = '" & MATHANG_ID & "'"

        'Ngày 2014-04-09: 
        'Người cập nhật: HT
        'Cập nhật hiển thị tình trạng phiếu theo BCTH
        strSql = "select (case when(NX = 'N') then N'DANH MỤC PHIẾU NHẬP' else N'DANH MỤC PHIẾU XUẤT' end) as NX,sophieu, (cast(day(NGAY) AS nvarchar(2)) + '/' + cast(month(NGAY)as nvarchar(2)) + '/'+ cast(YEAR(NGAY) as nvarchar(4))) as Ngay, tendvgn, tentcnx,slkh, slth, (case when(bcth = 1)then 'KH' when(bcth = 2)then 'TC' when(bcth = 3)then 'KHO' when(bcth = 4)then 'TCSL' end) as TT,dbo.LaySo_SoPhieu(sophieu) as so_sophieu, dbo.LayDuoi_SoPhieu(sophieu) as duoi_sophieu from viewNhapXuat " & _
                 "where MATHANG_ID = '" & MATHANG_ID & "' order by NX"
        Dim ds As DataSet
        ds = BaseDB.ExecSql_DataSet(strSql)
        dtgNhapXuat.DataSource = ds.Tables(0)
        dtgNhapXuat.RetrieveStructure()
        dtgNhapXuat.RootTable.Groups.Add(New Janus.Windows.GridEX.GridEXGroup(dtgNhapXuat.RootTable.Columns("NX")))
        dtgNhapXuat.RootTable.SortKeys.Add(New Janus.Windows.GridEX.GridEXSortKey(dtgNhapXuat.RootTable.Columns("duoi_sophieu"), System.Windows.Forms.SortOrder.Ascending))
        dtgNhapXuat.RootTable.SortKeys.Add(New Janus.Windows.GridEX.GridEXSortKey(dtgNhapXuat.RootTable.Columns("so_sophieu"), System.Windows.Forms.SortOrder.Ascending))
        'dtgNhapXuat.RootTable.Groups(0).Table.TotalRow = InheritableBoolean.True
        'dtgNhapXuat.RootTable.Groups(0).Table.Columns("SLKH").AggregateFunction = AggregateFunction.Sum
        'dtgNhapXuat.RootTable.Groups(0).Table.Columns("SLKH").FormatString = "#,###.##"
        'dtgNhapXuat.RootTable.Groups(0).Table.Columns("SLKH").TextAlignment = Janus.Windows.GridEX.TextAlignment.Far
        'dtgNhapXuat.RootTable.Groups(0).Table.Columns("SLTH").AggregateFunction = AggregateFunction.Sum
        'dtgNhapXuat.RootTable.Groups(0).Table.Columns("SLTH").FormatString = "#,###.##"
        'dtgNhapXuat.RootTable.Groups(0).Table.Columns("SLTH").TextAlignment = Janus.Windows.GridEX.TextAlignment.Far
        dtgNhapXuat.GroupByBoxVisible = False
        FormatGridStyledtgNhapXuat()
        '-------------------------------------------------------------------
        'Dim cl As New DataColumn("stt")
        'ds.Tables(0).Columns.Add(cl)
        'ds.Tables(0).DefaultView.Sort() = "sophieu DESC"
        'Dim i As Integer = 0
        'For Each row As DataRow In ds.Tables(0).Rows
        '    row("stt") = i
        '    i += 1
        'Next

        'olgridNhapXuat.BindData(ds, "Table")
        'If ds.Tables(0).Rows.Count > 0 Then
        '    FormatGridStyleolgridNhapXuat()
        '    Dim ColumnIndex As Integer = 0 'group theo nhom numcol=0

        '    '// set the group template to use, e.g. to sort alphabetically:
        '    olgridNhapXuat.GroupTemplate = New OutlookgGridDefaultGroup 'OutlookGridAlphabeticGroup()

        '    '// specify the column the Group will be associated with:
        '    olgridNhapXuat.GroupTemplate.Column = olgridNhapXuat.Columns(ColumnIndex)

        '    '// all groups in the list will be collapsed,
        '    '// so only the groups are displayed, not the items
        '    olgridNhapXuat.GroupTemplate.Collapsed = False
        '    '// sort the grid using the DataRowComparer object
        '    '// the DataRowComparer constructor takes two parameters,
        '    '// the column that will be sorted on, and the direction
        '    '// in which to sort (ascending or descending)
        '    Try
        '        olgridNhapXuat.Sort(New DataRowComparer(0, ListSortDirection.Ascending))
        '        'olgridNhapXuat.Sort(olgridNhapXuat.Columns(ColumnIndex), ListSortDirection.Ascending)
        '    Catch ex As Exception
        '        MsgBox(ex.Source + ex.Message)
        '    End Try
        'End If
    End Sub

    Public Sub FormatGridStyledtgNhapXuat()
        With dtgNhapXuat.RootTable
            With .Columns.Item("NX")
                .Caption = "Nhóm"
                .Visible = False
            End With
            With .Columns.Item("so_sophieu")
                .Visible = False
            End With
            With .Columns.Item("duoi_sophieu")
                .Visible = False
            End With
            With .Columns.Item("TT")
                .Width = 50
                .Caption = "TT"
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Center
            End With
            With .Columns.Item("ngay")
                .Width = 60
                .Caption = "Ngày"
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Center
            End With
            With .Columns.Item("sophieu")
                .Width = 60
                .Caption = "S.P"
                .CellStyle.ForeColor = Color.Blue
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Center
            End With

            With .Columns.Item("tendvgn")
                .Width = 240
                .Caption = "Đơn vị giao nhận"
            End With
            With .Columns.Item("tentcnx")
                .Width = 140
                .Caption = "Tính chất Nhập Xuất"
            End With
            With .Columns.Item("slkh")
                .Width = 100
                .Caption = "SLKH"
                .FormatString = "#,###.##"
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Far
            End With
            With .Columns.Item("slth")
                .Width = 100
                .Caption = "SLTH"
                .FormatString = "#,###.##"
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Far
            End With
        End With
    End Sub
#End Region

#Region "ListBox"
    ''Code: Tuan
    'Private Sub lbxLoHang_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxLoHang.DoubleClick
    '    If fCallPhieuNX IsNot Nothing Then
    '        If Not IsNothing(Me.lbxLoHang.SelectedItem) Then
    '            If fCallPhieuNX.Name Is frmPhieuNX.Name Then
    '                Dim frm As frmPhieuNX = CType(fCallPhieuNX, frmPhieuNX)
    '                Dim obj As CMatHang = Me.lbxLoHang.SelectedItem
    '                frm.AddMatHang(obj.MatHang_ID)
    '            ElseIf fCallPhieuNX.Name Is frmPhieuNXNoiBo.Name Then
    '                Dim frm As frmPhieuNXNoiBo = CType(fCallPhieuNX, frmPhieuNXNoiBo)
    '                Dim obj As CMatHang = Me.lbxLoHang.SelectedItem
    '                frm.AddMatHang(obj.MatHang_ID)
    '            ElseIf fCallPhieuNX.Name Is frmPhieuNXDCSS.Name Then
    '                Dim frm As frmPhieuNXDCSS = CType(fCallPhieuNX, frmPhieuNXDCSS)
    '                Dim obj As CMatHang = Me.lbxLoHang.SelectedItem
    '                frm.AddMatHang(obj.MatHang_ID)
    '            End If
    '        End If
    '    End If
    'End Sub

    Private Sub lbxLoHang_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lbxLoHang.KeyDown
        If e.KeyCode = Keys.Enter Then
            'lbxLoHang_DoubleClick(Nothing, Nothing)
        ElseIf e.KeyCode = Keys.Delete Then
            XoaLoHangCmnu_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub lbxLoHang_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lbxLoHang.DrawItem
        Try
            Dim brText As Brush
            e.DrawBackground()
            Dim objDT As CMatHang = Me.lbxLoHang.Items(e.Index)  'Me.lstItems.SelectedItem
            If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
                brText = Brushes.White
            ElseIf objDT.SLTonKH = 0 Then
                brText = Brushes.Gray
            Else
                brText = Brushes.Blue
            End If
            'If (flagHSD) Then
            '    Dim hsd As String
            '    If (objDT.HanSD <> "") Then
            '        hsd = objDT.HanSD
            '    Else
            '        hsd = "H12/49"
            '    End If
            '    If (Not SoSanhHSD(hsd, mtbHSD.Text)) Then
            '        brText = Brushes.Red
            '    End If
            'End If
            If objDT.Lo <> "" Then
                e.Graphics.DrawString(objDT.Ten + " - " + objDT.Lo, Me.lbxLoHang.Font, brText, e.Bounds.X + 5, e.Bounds.Y)
            Else
                e.Graphics.DrawString(objDT.Ten, Me.lbxLoHang.Font, brText, e.Bounds.X + 5, e.Bounds.Y)
            End If

        Catch ex As Exception
        Finally
            'If Not brColor Is Nothing Then
            'brColor.Dispose()
            'End If
        End Try
    End Sub

    Private Sub lbxLoHang_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxLoHang.SelectedIndexChanged
        Try
            If lbxLoHang.Items.Count <> 0 Then
                Dim obj_MatHang As CMatHang = lbxLoHang.SelectedItem
                txtTenHang.Text = obj_MatHang.Ten
                txtMa_Lo.Text = obj_MatHang.Ma + "." + obj_MatHang.Code + "   " + obj_MatHang.Lo
                txtNgTTon.Text = obj_MatHang.NgTTon.ToString(FormatDateStyle())
                txtCCL.Text = obj_MatHang.H
                txtHSD.Text = obj_MatHang.HanSD
                txtDonGia.Text = obj_MatHang.Gle.ToString(FormatMoneyStyle())
                txtLoaiKho.Text = CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.LOAIKHO, obj_MatHang.LK).Ten
                txtTonKH.Text = obj_MatHang.SLTonKH.ToString(FormatQuantityStyle())
                txtTonTH.Text = obj_MatHang.SLTonTH.ToString(FormatQuantityStyle())
                txtKiemKeDauNam.Text = obj_MatHang.SLKK11.ToString(FormatQuantityStyle())
                txtLuyTichNhapKH.Text = obj_MatHang.SLNDKKH.ToString(FormatQuantityStyle())
                txtLuyTichXuatKH.Text = obj_MatHang.SLXDKKH.ToString(FormatQuantityStyle())
                txtLuyTichNhapTH.Text = obj_MatHang.SLNDKTH.ToString(FormatQuantityStyle())
                txtLuyTichXuatTH.Text = obj_MatHang.SLXDKTH.ToString(FormatQuantityStyle())
                txtSLSS.Text = obj_MatHang.SLTonSS.ToString(FormatQuantityStyle())
                txtSLSSDauNam.Text = obj_MatHang.SLSS11.ToString(FormatQuantityStyle())
                txtDVT.Text = obj_MatHang.DVT
                KhoiTaodtgPhieuNhapXuat(obj_MatHang.MatHang_ID)
            End If
        Catch ex As Exception
            ShowMessages(ex.ToString())
        End Try
    End Sub

#End Region

#Region "Event, btnLoc"
    Private Sub btnLoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoc.Click
        If (cbbKhoHang.SelectedIndex <> -1) Then
            Dim frm As New frmLocLoHang(Me, CType(cbbKhoHang.SelectedItem, CDoiTuong).KH)
            frm.ShowDialog()
            KhoiTaoDMLoHang(GetstrLoc())
        End If
    End Sub

    Private Function GetstrLoc() As String
        Dim strsql As String = ""

        'loai kho
        If (mDMLoaiKho <> "") Then
            Dim str() As String
            str = mDMLoaiKho.Split("/")
            strsql &= "("
            For Each s As String In str
                If (strsql.Length > 1) Then ' vi them ( vao roi
                    strsql &= " or (LK = '" & s & "')"
                Else
                    strsql &= " (LK = '" & s & "')"
                End If
            Next
            strsql &= ")"
        End If

        'ten hang
        If (mTenHang <> "") Then
            If (strsql.Length > 0) Then
                strsql &= " and ten like N'" & mTenHang & "%'"
            Else
                strsql &= " ten like N'" & mTenHang & "%'"
            End If
        End If

        'ma hang
        If (mMaHang <> "") Then
            If (mMaHang.Substring(7, 7) = "0000000") Then 'loai thuoc
                'xuli chuoi: loai bo nhung ki tu 0 dung sau de lay duoc loai thuoc
                Dim loaithuoc As String = mMaHang.Substring(0, 7)
                For i As Integer = 0 To 6
                    If (loaithuoc.Chars(6 - i) = "0") Then
                        loaithuoc = loaithuoc.Remove(6 - i, 1)
                    Else
                        Exit For
                    End If
                Next
                'lay left
                If (strsql.Length > 0) Then
                    strsql &= " and left(ma," + CStr(loaithuoc.Length) + ") = '" + loaithuoc + "'"
                Else
                    strsql &= " left(ma," + CStr(loaithuoc.Length) + ") = '" + loaithuoc + "'"
                End If
            Else 'thuoc
                If (strsql.Length > 0) Then
                    strsql &= " and ma = '" + mMaHang + "'"
                Else
                    strsql &= " ma = '" + mMaHang + "'"
                End If
            End If

        End If
        'lohang
        If (mLoHang <> "") Then
            If (strsql.Length > 0) Then
                strsql &= " and lo = '" + mLoHang + "'"
            Else
                strsql &= " lo = '" + mLoHang + "'"
            End If
        End If
        'cap chat luong
        If (mCCL <> "") Then
            If (strsql.Length > 0) Then
                strsql &= " and h = '" + mCCL + "'"
            Else
                strsql &= " h = '" + mCCL + "'"
            End If
        End If
        'han su dung

        If (mSSHSD <> "") Then
            'chuyen doi ngay trong sql

            If (strsql.Length > 0) Then
                strsql &= " and convert(datetime,hansd_hoanchinh)" + mSSHSD + "'" & Format(mHSD, "M/d/yyyy") & "'"
            Else
                strsql &= " convert(datetime,hansd_hoanchinh)" + mSSHSD + "'" & Format(mHSD, "M/d/yyyy") & "'"
            End If
        End If

        If (mSSSLTon <> "") And (mSLTon <> "") Then
            If (strsql.Length > 0) Then
                strsql &= " and SLTonkh" + mSSSLTon + "'" + mSLTon + "'"
            Else
                strsql &= " SLTonkh" + mSSSLTon + "'" + mSLTon + "'"
            End If
        End If
        If (strsql.Length > 0) Then
            'Nếu thuộc tính chọn là <Chọn tất cả> (mã = 0001)
            If CType(cbbKhoHang.SelectedItem, CDoiTuong).KH <> "0001" Then
                strsql = " where kho = '" + CType(cbbKhoHang.SelectedItem, CDoiTuong).KH + "' and " & strsql
            Else
                strsql = " where " & strsql
            End If
        Else
            If CType(cbbKhoHang.SelectedItem, CDoiTuong).KH <> "0001" Then
                strsql = " where kho = '" + CType(cbbKhoHang.SelectedItem, CDoiTuong).KH + "'"
            End If
        End If
        Return strsql
    End Function

    Private Sub cbbKhoHang_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbbKhoHang.SelectedIndexChanged
        If (cbbKhoHang.SelectedIndex <> -1) Then
            KhoiTaoDMLoHang(GetstrLoc())
            'InitListView(GetstrLoc())
        End If
    End Sub

    'Private Sub mtbHSD_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    If (e.KeyCode = Keys.Enter) Then
    '        If (KiemTraNhap_mtbHSD()) Then
    '            flagHSD = True
    '            lbxLoHang.Refresh()
    '            mtbHSD.ReadOnly = True
    '        End If
    '    End If
    '    If (e.KeyCode = Keys.Delete) And (e.Modifiers = Keys.Control) Then
    '        flagHSD = False
    '        lbxLoHang.Refresh()
    '        mtbHSD.ReadOnly = False
    '        mtbHSD.Text = ""
    '    End If
    'End Sub

    'Public Function KiemTraNhap_mtbHSD() As Boolean
    '    Try
    '        Dim kq As Boolean = True
    '        Dim hsd As String
    '        hsd = mtbHSD.Text.Substring(1)
    '        Dim str() As String
    '        str = hsd.Split("/")
    '        If (str(0).Trim <> "") Then
    '            If (CInt(str(0)) > 12) Or (CInt(str(0)) < 1) Then
    '                kq = False
    '                MsgBox("Bạn nhập tháng HSD cần tìm chưa đúng")
    '            End If
    '        Else
    '            kq = False
    '            MsgBox("Bạn chưa nhập tháng HSD cần tìm ")
    '        End If
    '        If (str(1).Trim.Length <> 2) Then
    '            MsgBox("Bạn nhập năm HSD cần tìm chưa đúng")
    '            kq = False
    '        End If
    '        Return kq
    '    Catch ex As Exception
    '        MsgBox("Bạn nhập HSD cần tìm chưa đúng")
    '    End Try
    'End Function

    Public Function SoSanhHSD(ByVal HSDa As String, ByVal HSDb As String) As Boolean
        ' true : a>b else false
        Dim kq As Boolean = False
        Dim thanga, nama, thangb, namb As Integer
        thanga = CInt(HSDa.Substring(1, 2))
        nama = CInt(HSDa.Substring(4, 2))
        thangb = CInt(HSDb.Substring(1, 2))
        namb = CInt(HSDb.Substring(4, 2))
        If (nama > namb) Then
            kq = True
        ElseIf (nama = namb) Then
            If (thanga > thangb) Then
                kq = True
            End If
        End If
        Return kq
    End Function


#End Region

#Region "btn BoLoc, ThongKe"
    ''' <summary>
    ''' HT Code:
    ''' Ngày 2014-05-25
    ''' Thêm chức năng in phiếu theo SLKH
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnThongKeNhapXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThongKeNhapXuat.Click
        'bieu thong ke nhap xuat
        Dim frmTKNX As New frmThongKeNhapXuat
        frmTKNX.ShowDialog()
        Dim isPrint As Boolean = frmTKNX.IsPrint
        Dim isSLKH As Boolean = frmTKNX.IsSLKH

        Dim strsql As String
        strsql = "select * from viewNhapXuat where mathang_id = '" & CStr(CType(lbxLoHang.SelectedItem, CMatHang).MatHang_ID) & "' order by NX"
        Dim ds As DataSet
        ds = BaseDB.ExecSql_DataSet(strsql)
        If isPrint Then
            Dim nnreport As New rptBaoCaoThongKeNhapXuatTheoLoHang
            nnreport.SetDataSource(ds.Tables(0))
            nnreport.SetParameterValue(0, txtLoaiKho.Text)
            strsql = "select count(*) from tblDPKK where Year(ngaykk)= '" & CStr(Now.Year) & "'"
            Dim value As Integer = 0
            value = BaseDB.ExecSql_DataValue(strsql)
            nnreport.SetParameterValue(1, value)
            nnreport.SetParameterValue(2, isSLKH)
            Dim frm As New frmReportViewer(nnreport)
            frm.ShowDialog()
        Else
            'xuat ra excel
            Dim mFileName2 As String = Application.StartupPath & "\TempTHONGKENHAPXUAT.xls"
            ToEXCEL(mFileName2, ds, isPrint, isSLKH)
        End If
    End Sub

    Private Sub btnBoLoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBoLoc.Click
        Dim strWhere As String
        strWhere = "where kho = '" + CType(cbbKhoHang.SelectedItem, CDoiTuong).KH + "'"
        KhoiTaoDMLoHang(strWhere)

        'set lai gia tri loc da luu
        mMaHang = ""
        mLoHang = ""
        mCCL = ""
        mSSHSD = ""
        mHSD = Now.Date
        mSSSLTon = ""
        mSLTon = ""
        mDMLoaiKho = ""
    End Sub
#End Region

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

#Region "Excel"
    Private Sub ToEXCEL(ByVal mFileName2 As String, ByVal m_ds As DataSet, ByVal isPrint As Boolean, ByVal isSLKH As Boolean)
        Dim mFileName As String = Application.StartupPath & "\THONGKENHAPXUAT.xls"

        System.IO.File.Copy(mFileName, mFileName2, True)
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        Dim mRange As Excel.Range

        xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
        xlBook = CType(xlApp.Workbooks.Open(mFileName2), Excel.Workbook)

        xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)

        xlSheet.Activate()
        xlSheet.Application.Visible = True

        'dau phieu
        Dim objMatHang As CMatHang = CType(lbxLoHang.SelectedItem, CMatHang)
        xlSheet.Cells(2, 3) = "MẶT HÀNG : " + objMatHang.Ma + "-" + objMatHang.Code
        xlSheet.Cells(3, 3) = objMatHang.Ten
        mRange = xlSheet.Cells(3, 3)
        With mRange.Font
            .Size = 16
        End With
        xlSheet.Cells(4, 2) = "Đơn vị tính : " + objMatHang.DVT
        mRange = xlSheet.Cells(3, 4)
        With mRange.Font
            .Size = 16
            .Bold = True
        End With
        xlSheet.Cells(5, 2) = "Lô : " + objMatHang.Lo
        xlSheet.Cells(6, 2) = "Loại kho : " + CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.LOAIKHO, objMatHang.LK).Ten

        xlSheet.Cells(4, 4) = "Chất lượng : " + objMatHang.H
        xlSheet.Cells(5, 4) = "Hạn dùng : " + objMatHang.HanSD + "  Giá lẻ : " + CStr(objMatHang.Gle)
        xlSheet.Cells(6, 5) = "Kho : " + CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.KHO, objMatHang.Kho).Ten

        'SLKH.......
        'Code HT: Nếu là in cả SLTH
        If Not isSLKH Then
            xlSheet.Cells(8, 4) = objMatHang.SLKK11.ToString(FormatQuantityStyle())
            xlSheet.Cells(9, 4) = objMatHang.SLNDKTH.ToString(FormatQuantityStyle())
            xlSheet.Cells(10, 4) = objMatHang.SLXDKTH.ToString(FormatQuantityStyle())
            xlSheet.Cells(11, 4) = objMatHang.SLTonKH.ToString(FormatQuantityStyle())
            xlSheet.Cells(12, 4) = objMatHang.SLTonTH.ToString(FormatQuantityStyle())
        Else
            xlSheet.Cells(8, 4) = objMatHang.SLKK11.ToString(FormatQuantityStyle())
            xlSheet.Cells(9, 4) = objMatHang.SLNDKKH.ToString(FormatQuantityStyle())
            xlSheet.Cells(10, 4) = objMatHang.SLXDKKH.ToString(FormatQuantityStyle())
            xlSheet.Cells(11, 4) = objMatHang.SLTonKH.ToString(FormatQuantityStyle())
        End If

        'Thanh tien ......
        Dim gle As Double = objMatHang.Gle
        xlSheet.Cells(8, 6) = (objMatHang.SLKK11 * gle).ToString(FormatMoneyStyle())

        'Code HT: Nếu là in cả SLTH
        If Not isSLKH Then
            xlSheet.Cells(9, 6) = (objMatHang.SLNDKTH * gle).ToString(FormatMoneyStyle())
            xlSheet.Cells(10, 6) = (objMatHang.SLXDKTH * gle).ToString(FormatMoneyStyle())
            xlSheet.Cells(11, 6) = (objMatHang.SLTonKH * gle).ToString(FormatMoneyStyle())
            xlSheet.Cells(12, 6) = (objMatHang.SLTonTH * gle).ToString(FormatMoneyStyle())
        Else
            xlSheet.Cells(9, 6) = (objMatHang.SLNDKKH * gle).ToString(FormatMoneyStyle())
            xlSheet.Cells(10, 6) = (objMatHang.SLXDKKH * gle).ToString(FormatMoneyStyle())
            xlSheet.Cells(11, 6) = (objMatHang.SLTonKH * gle).ToString(FormatMoneyStyle())
        End If



        Dim iRow0 As Integer = 15
        Dim len, i As Integer
        Dim ngay_vietphieu, nx, so_phieu, dvgn, ngay_nhan As String
        Dim slth, slkh As Double
        Dim nhap, xuat, so_donvinhap, so_donvixuat As Integer
        Dim tempdvgn_nhap, tempdvgn_xuat As String
        tempdvgn_nhap = ""
        tempdvgn_xuat = ""
        'Dim flag As Boolean = False

        i = 0
        '(i chay trong ds, j chay trong excel)
        len = m_ds.Tables(0).Rows.Count
        While (i < len)
            If (i >= len) Then
                Exit While
            End If
            ngay_vietphieu = ChuanHoaChuoi(m_ds.Tables(0).Rows.Item(i).Item("NGAY"))
            nx = m_ds.Tables(0).Rows.Item(i).Item("NX")
            so_phieu = m_ds.Tables(0).Rows.Item(i).Item("SOPHIEU")
            dvgn = ChuanHoaChuoi(m_ds.Tables(0).Rows.Item(i).Item("DVGN"))
            ngay_nhan = ChuanHoaChuoi(m_ds.Tables(0).Rows.Item(i).Item("NGAYGN"))
            slth = m_ds.Tables(0).Rows.Item(i).Item("SLTH")
            slkh = m_ds.Tables(0).Rows.Item(i).Item("SLKH")

            If (nx = "N") Then
                nhap += 1
                If (dvgn <> tempdvgn_nhap) Then
                    so_donvinhap += 1
                    tempdvgn_nhap = dvgn
                End If
            Else
                xuat += 1
                If (dvgn <> tempdvgn_xuat) Then
                    so_donvixuat += 1
                    tempdvgn_xuat = dvgn
                End If
            End If

            xlSheet.Cells(iRow0 + i, 1) = GetDate(ngay_vietphieu)
            xlSheet.Cells(iRow0 + i, 2) = nx + "  " + so_phieu
            xlSheet.Cells(iRow0 + i, 3) = CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.DONVI, dvgn).Ten
            xlSheet.Cells(iRow0 + i, 4) = GetDate(ngay_nhan)
            xlSheet.Cells(iRow0 + i, 5) = slkh.ToString(FormatQuantityStyle())
            'Code HT: Nếu là in cả SLTH
            If Not isSLKH Then
                xlSheet.Cells(iRow0 + i, 6) = slth.ToString(FormatQuantityStyle())
            End If

            '
            i += 1
            If (i >= len) Then
                Exit While
            End If
        End While
        'in tong cong
        If (i <> 0) Then
            Dim iRowsCount = i - 1
            Dim mColsCount = 6
            'col1 AlignCenter

            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 1), xlSheet.Cells(iRow0 + iRowsCount, 1))
            With mRange
                .HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            End With
            '
            'col4 AlignCenter
            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 4), xlSheet.Cells(iRow0 + iRowsCount, 4))
            With mRange
                .HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            End With
            '
            'col5,6 AlignRight
            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 5), xlSheet.Cells(iRow0 + iRowsCount, 6))
            With mRange
                .HorizontalAlignment = Excel.XlHAlign.xlHAlignRight
            End With
            '
            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 1), xlSheet.Cells(iRow0 + iRowsCount, mColsCount))
            With mRange
                .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
            End With
            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 1), xlSheet.Cells(iRow0 + iRowsCount, mColsCount))
            With mRange.Borders(Excel.XlBordersIndex.xlEdgeLeft)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
            With mRange.Borders(Excel.XlBordersIndex.xlEdgeTop)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
            With mRange.Borders(Excel.XlBordersIndex.xlEdgeBottom)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
            With mRange.Borders(Excel.XlBordersIndex.xlEdgeRight)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
            With mRange.Borders(Excel.XlBordersIndex.xlInsideVertical)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With

            With mRange.Borders(Excel.XlBordersIndex.xlInsideHorizontal)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
        End If
        xlApp.ActiveWindow.Visible = False
        xlApp.Windows(1).Activate()
        '
        'duoi phieu
        xlSheet.Cells(iRow0 + i + 1, 2) = "Số lượng phiếu Nhập : " + CStr(nhap)
        xlSheet.Cells(iRow0 + i + 2, 2) = "Số lượng phiếu Xuất : " + CStr(xuat)
        xlSheet.Cells(iRow0 + i + 1, 4) = "Nhập hàng từ: " + CStr(so_donvinhap)
        xlSheet.Cells(iRow0 + i + 2, 4) = "Xuất hàng cho : " + CStr(so_donvixuat)
        xlSheet.Cells(iRow0 + i + 4, 5) = "In Ngày " + CStr(Now.Day) + "/" + CStr(Now.Month) + "/" + CStr(Now.Year)
    End Sub

    Function GetDate(ByVal ngay As Object) As String
        If (Not ngay Is DBNull.Value) Then
            Try
                Dim n As Date = CDate(ngay)
                Return CStr(n.Day) + "/" + CStr(n.Month) + "/" + CStr(n.Year)
            Catch ex As Exception
                'neu ko ep duoc kieu ngay thi tra ve du lieu hien co 
                Return CStr(ngay)
            End Try
        End If
        Return ""
    End Function
#End Region

    Private Sub XoaLoHangCmnu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XoaLoHangCmnu.Click

        If (dtgNhapXuat.RowCount > 0) Then
            ShowMessages("Bạn không được xóa lô hàng này vì liên quan đến phiếu Nhập Xuất")
            Exit Sub
        End If
        Try
            Dim obj_MatHang As CMatHang = lbxLoHang.SelectedItem
            If (obj_MatHang.SLKK11 = 0) And _
               (obj_MatHang.SLNDKKH = 0) And _
               (obj_MatHang.SLNDKTH = 0) And _
               (obj_MatHang.SLXDKKH = 0) And _
               (obj_MatHang.SLXDKTH = 0) And _
               (obj_MatHang.SLTonKH = 0) And _
               (obj_MatHang.SLTonTH = 0) Then
                Dim index = lbxLoHang.SelectedIndex
                CMatHangs.DeleteMatHang(obj_MatHang)
                KhoiTaoDMLoHang(GetstrLoc())
                lbxLoHang.SelectedIndex = index - 1
                lbxLoHang.Focus()
                '==========================
                'Write log
                CLogs.WriteLog_LoHang(enumAction.Xoa, obj_MatHang)
                Exit Sub
            Else
                ShowMessages("Mặt hàng còn số lượng tồn > 0. Không thể xóa được.")
                Exit Sub
            End If
        Catch ex As Exception
            MsgBox(ex.Source)
        End Try
        'chua xet so phieu nhap xuat co trong mat hang nay.
        'Dim strsql As String
        'strsql = "select count(*) from from viewNhapXuat " & _
        '          "where MATHANG_ID = '" & MATHANG_ID & "'"
        ShowMessages("Số liệu này khác 0 nên bạn không được xóa")
    End Sub

#Region "ListView"
    'Private Sub frmQuanLyLoHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '    With lvDanhMuc
    '        .HideSelection = False ' = True de ton tai truong hop e.state <> ListViewItemStates.Selected
    '        .OwnerDraw = False
    '        .FullRowSelect = True
    '        .GridLines = True
    '        .View = View.Details
    '        .Columns.Add("Tên", 80, HorizontalAlignment.Left)
    '        .Columns.Add("Lô hàng", 80, HorizontalAlignment.Left)
    '        .Columns.Add("Số lượng", lvDanhMuc.Width - lvDanhMuc.Columns(0).Width - lvDanhMuc.Columns(1).Width - 20, HorizontalAlignment.Left)
    '    End With
    'End Sub


    'Private Sub lvDanhMuc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvDanhMuc.SelectedIndexChanged
    '    Try
    '        If lvDanhMuc.SelectedItems.Count > 0 Then
    '            Dim obj_MatHang As CMatHang = CMatHangs.GetMatHangByMa(CInt(lvDanhMuc.SelectedItems(0).Name))
    '            txtTenHang.Text = obj_MatHang.Ten
    '            txtMa_Lo.Text = obj_MatHang.Ma + "." + obj_MatHang.Code + "   " + obj_MatHang.Lo
    '            txtNgTTon.Text = obj_MatHang.NgTTon.ToString(FormatDateStyle())
    '            txtCCL.Text = obj_MatHang.H
    '            txtHSD.Text = obj_MatHang.HanSD
    '            txtDonGia.Text = obj_MatHang.Gle.ToString(FormatMoneyStyle())
    '            txtLoaiKho.Text = CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.LOAIKHO, obj_MatHang.LK).Ten
    '            txtTonKH.Text = obj_MatHang.SLTonKH.ToString(FormatQuantityStyle())
    '            txtTonTH.Text = obj_MatHang.SLTonTH.ToString(FormatQuantityStyle())
    '            txtKiemKeDauNam.Text = obj_MatHang.SLKK11.ToString(FormatQuantityStyle())
    '            txtLuyTichNhapKH.Text = obj_MatHang.SLNDKKH.ToString(FormatQuantityStyle())
    '            txtLuyTichXuatKH.Text = obj_MatHang.SLXDKKH.ToString(FormatQuantityStyle())
    '            txtLuyTichNhapTH.Text = obj_MatHang.SLNDKTH.ToString(FormatQuantityStyle())
    '            txtLuyTichXuatTH.Text = obj_MatHang.SLXDKTH.ToString(FormatQuantityStyle())
    '            txtSLSS.Text = obj_MatHang.SLTonSS.ToString(FormatQuantityStyle())
    '            txtSLSSDauNam.Text = obj_MatHang.SLSS11.ToString(FormatQuantityStyle())
    '            txtDVT.Text = obj_MatHang.DVT
    '            KhoiTaodtgPhieuNhapXuat(obj_MatHang.MatHang_ID)
    '        End If
    '    Catch ex As Exception
    '        ShowMessages(ex.ToString())
    '    End Try
    'End Sub

    'Public Sub InitListView(ByVal strwhere As String)
    '    Dim aryDTs As ArrayList = CMatHangs.GetMatHangList(strwhere)
    '    lvDanhMuc.Items.Clear()
    '    lblTongMatHang.Text = aryDTs.Count.ToString + " Mặt Hàng"
    '    If aryDTs.Count > 0 Then
    '        Dim item As ListViewItem
    '        Dim subItem As ListViewItem.ListViewSubItem
    '        For Each obj As CMatHang In aryDTs
    '            item = New ListViewItem(obj.Ten)
    '            subItem = New ListViewItem.ListViewSubItem()
    '            'subItem.Name = .column_mahieu
    '            subItem.Text = obj.Lo
    '            item.SubItems.Add(subItem)

    '            subItem = New ListViewItem.ListViewSubItem()
    '            'subItem.Name = CBanDoCollection.column_ten_bando
    '            subItem.Text = obj.SLTonKH
    '            item.SubItems.Add(subItem)

    '            'add key of item
    '            item.Name = obj.MatHang_ID
    '            lvDanhMuc.Items.Add(item)
    '        Next
    '        lvDanhMuc.Focus()
    '        lvDanhMuc.Items(0).Selected = True
    '    Else
    '        txtTenHang.Text = ""
    '        txtMa_Lo.Text = ""
    '        txtNgTTon.Text = ""
    '        txtCCL.Text = ""
    '        txtHSD.Text = ""
    '        txtDonGia.Text = ""
    '        txtTonKH.Text = ""
    '        txtTonTH.Text = ""
    '    End If
    'End Sub

#Region "Draw Item"

    'Private Sub lvDanhMuc_DrawColumnHeader(ByVal sender As Object, ByVal e As DrawListViewColumnHeaderEventArgs) Handles lvDanhMuc.DrawColumnHeader
    '    Dim sf As New StringFormat()
    '    Try
    '        sf.Alignment = StringAlignment.Center
    '        ' Draw the standard header background.
    '        e.DrawBackground()
    '        ' Draw the header text.
    '        'Dim headerFont As New Font("Courier New", 9, FontStyle.Bold)
    '        e.Graphics.DrawString(e.Header.Text, e.Font, Brushes.Black, e.Bounds, sf)
    '    Catch ex As Exception
    '    Finally
    '        sf.Dispose()
    '    End Try
    'End Sub

    '' Draws the backgrounds for entire ListView items.
    'Private Sub lvDanhMuc_DrawItem(ByVal sender As Object, ByVal e As DrawListViewItemEventArgs) Handles lvDanhMuc.DrawItem
    '    If Not (e.State And ListViewItemStates.Selected) = 0 Then
    '        'Dim objDT As CMatHang = Me.lvDanhMuc.Items(e.Index)  'Me.lstItems.SelectedItem
    '        'If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
    '        '    brText = Brushes.White
    '        'ElseIf objDT.SLTonKH = 0 Then
    '        '    brText = Brushes.Gray
    '        'Else
    '        '    brText = Brushes.Blue
    '        'End If
    '        ' Draw the background for a selected item.
    '        e.Graphics.FillRectangle(Brushes.DarkBlue, e.Bounds)
    '        e.DrawFocusRectangle()
    '    End If
    'End Sub

    'Private Sub lvDanhMuc_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) Handles lvDanhMuc.DrawSubItem
    '    Dim flags As TextFormatFlags = TextFormatFlags.Left
    '    Dim sf As New StringFormat()
    '    Try
    '        '' Store the column text alignment, letting it default
    '        '' to Left if it has not been set to Center or Right.
    '        Select Case e.Header.TextAlign
    '            Case HorizontalAlignment.Left
    '                sf.Alignment = StringAlignment.Near
    '                flags = TextFormatFlags.Left
    '            Case HorizontalAlignment.Center
    '                sf.Alignment = StringAlignment.Center
    '                flags = TextFormatFlags.HorizontalCenter
    '            Case HorizontalAlignment.Right
    '                sf.Alignment = StringAlignment.Far
    '                flags = TextFormatFlags.Right
    '        End Select

    '        ' Draw normal text for a subitem with a nonnegative 
    '        ' or nonnumerical value.
    '        If Not (e.ItemState And ListViewItemStates.Selected) = 0 Then
    '            e.Graphics.DrawString(e.SubItem.Text, Me.lvDanhMuc.Font, Brushes.White, e.Bounds, sf)
    '        Else
    '            e.DrawText()
    '        End If
    '    Finally
    '        sf.Dispose()
    '    End Try

    'End Sub
#End Region

#End Region

    Private Sub frmQuanLyLoHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not g_User.IsAdmin And g_User.Ban_ID <> enumPHANQUYEN.KEHOACH Then
            lbxLoHang.ContextMenuStrip = Nothing
        End If
    End Sub

    Private Sub dtgNhapXuat_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles dtgNhapXuat.Paint

    End Sub

    Private Sub dtgNhapXuat_LoadingRow(ByVal sender As System.Object, ByVal e As Janus.Windows.GridEX.RowLoadEventArgs) Handles dtgNhapXuat.LoadingRow
        If e.Row.RowType = Janus.Windows.GridEX.RowType.Record Then
            Dim brText As Color
            Dim style As Janus.Windows.GridEX.GridEXFormatStyle = New Janus.Windows.GridEX.GridEXFormatStyle()
            Select Case e.Row.Cells("TT").Value
                Case "KH" 'Tình trạng phiếu: Kế hoạch đã xác nhận
                    brText = Color.Black
                Case "KHO" 'Tình trạng phiếu: Tài chính tiền đã tiền xác nhận
                    brText = Color.Green
                Case "TCT" 'Tình trạng phiếu: Tài chính tiền đã tiền xác nhận
                    brText = Color.Blue
                Case "TCSL" 'Tình trạng phiếu: Tài chính số lượng đã tiền xác nhận
                    brText = Color.Violet
                Case Else 'TC1, TC2, TC3, TC4
                    brText = Color.DarkOrange
            End Select
            style.ForeColor = brText
            e.Row.RowStyle = style
        End If
    End Sub
End Class